import pygame
from MazeData import MazeData,MazeData2,MazeData3


class MazeGame(object):
    def __init__(self, row, col):
        self.row = row
        self.col = col
        self.size = 9
        self.screenWidth = self.col * self.size
        self.screenHeight = self.row * self.size
        self.direction = ((-1, 0), (0, -1), (1, 0), (0, 1))
        self.screen = pygame.display.set_mode((self.screenWidth, self.screenHeight))
        pygame.display.set_caption("迷宫游戏")
        self.mazeData = MazeData3(row, col)
        self.update_GUI()

    def play(self):
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_g:
                        if self.go(1, 0):
                            print("Succeed!")
                        else:
                            print("此迷宫无解！")
                    if event.key == pygame.K_m:
                        self.mazeData.merge()
                        print(self.mazeData.data)
                        print(self.mazeData.visited)
                        self.update_GUI()
                    if event.key == pygame.K_r:
                        self.mazeData.generate_data()
                        self.update_GUI()

    def go(self, row, col):
        path = []
        stack = [[row, col]]
        while stack:
            row, col = stack.pop(0)
            Drawtools.DrawRect(self.screen, row, col, self.size, (255, 255, 0))
            for dCol, dRow in self.direction:
                if self.mazeData.isInArea(row + dRow, col + dCol) and (
                        not self.mazeData.visited[row + dRow][col + dCol] and (
                        self.mazeData.data[row + dRow][col + dCol] == ' ')):
                    self.mazeData.visited[row + dRow][col + dCol] = True
                    stack.append([row + dRow, col + dCol])
                    path.append(([row+dRow,col+dCol],[row, col]))
                    if [row + dRow, col + dCol] == self.mazeData.exit:
                        Drawtools.DrawRect(self.screen, row + dRow, col + dCol, self.size, (255, 0, 0))
                        nextPoint = [row+dRow,col+dCol]
                        Done = False
                        while not Done:
                            for nPoint, pPoint in path[::-1]:
                                if nextPoint == nPoint:
                                    nextPoint = pPoint
                                    Drawtools.DrawRect(self.screen, nextPoint[0], nextPoint[1], self.size, (255, 0, 0))
                                    break
                            if nextPoint == self.mazeData.entrance:
                                Drawtools.DrawRect(self.screen, nextPoint[0], nextPoint[1], self.size, (255, 0, 0))
                                Done = True
                        return True
        return False

    def update_GUI(self):
        self.screen.fill((255, 255, 255))
        for i, row in enumerate(self.mazeData.data):
            for j, col in enumerate(row):
                if col == '#':
                    cell = pygame.Rect(j * self.size, i * self.size, self.size, self.size)
                    pygame.draw.rect(self.screen, (10, 10, 200), cell)
        pygame.display.update()


class Drawtools(object):
    @staticmethod
    def DrawRect(screen, row, col, size, color):
        cell = pygame.Rect(col * size, row * size, size, size)
        pygame.draw.rect(screen, color, cell)
        pygame.display.update()


if __name__ == "__main__":
    maze = MazeGame(45, 47)
    maze.play()
